package Utils;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.log4j.Logger;

public class YiYouSMS {
	static Logger logger = Logger.getLogger(YiYouSMS.class);
	public static void main(String[] args) {
		try {
			/*
			 * String message="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
			 * "<infos>"+ "<info>"+ "<msg_id><![CDATA[123]]></msg_id>"+
			 * "<password><![CDATA[7869]]></password>"+
			 * "<src_tele_num><![CDATA[106573066104]]></src_tele_num>"+
			 * "<dest_tele_num><![CDATA[1358602249]]></dest_tele_num>"+
			 * "<msg><![CDATA[���1]]></msg>"+ "</info>"+ "</infos>";
			 */
//			String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
//					+ "<infos>" + "<info>"
//					+ "<msg_id><![CDATA[-1]]></msg_id>"
//					+ "<password><![CDATA[18674]]></password>"
//					+ "<src_tele_num><![CDATA[106573037333]]></src_tele_num>"
//					+ "<dest_tele_num><![CDATA[13559455537]]></dest_tele_num>"
//					+ "<msg><![CDATA[短信测试]]></msg>" + "</info>" + "</infos>";
//
//			String ret = send_msg("111.1.15.162", "ryywz037333", message);// 
//			System.out.println("短信发送结果： " + ret);
//			ret = receipt_all("ryywz037333");//
//			String[] result = ret.split(",");
//			System.out.println("短信查询结果： " + result[3]);
//			ret = rt_receipt("wz999999", "123");//
//			System.out.println("return value is " + ret);
			sendMsg("测试测试","13559455537");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 发送短信（易邮）
	 * @param msg
	 * @param phone
	 * @return
	 */
	public static long sendMsg(String msg,String phone){
		String result = null;
		String[] status = null;
		String password = String.valueOf(Integer.parseInt(phone.substring(7))*3+2063);
		try {
			result = send_msg("111.1.15.162", "ryywz037333", add_msg_t()+add_msg_b("-1",password,"106573037333",phone,msg)+add_msg_w());
			System.out.println("短信发送结果： " + result);
			result=receipt_all("ryywz037333");
			status = result.split(",");
			result = status[3];
			System.out.println("短信查询结果： " + result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		if("发送成功;".equals(result)){
			return Long.parseLong(status[0]);
		}else{
//			logger.info("[Error]YiYouSMS send fail response: "+result);
			return -1;
		}
	}

	public static String send_msg(String wbip, String dbuser, String message)
			throws Exception {

		// //System.setProperty("http.proxyHost", "10.70.237.88");
		// //System.setProperty("http.proxyPort", "8080");
		String endpoint = "http://111.1.15.162/webservice/services/sendmsg";
		Service service = new Service();
		Call call = null;

		call = (Call) service.createCall();

		call.setOperationName(new QName(
				"http://111.1.15.162/webservice/services/sendmsg", "sendmsg"));
		call.setTargetEndpointAddress(new java.net.URL(endpoint));
		String ret = (String) call.invoke(new Object[] { dbuser, message });
		// System.out.println("return value is " + ret);
		return ret;
	}

	public static String send_msg_st(String wbip, String dbuser, String message)
			throws Exception {

		// System.setProperty("http.proxyHost", "10.70.237.88");
		// System.setProperty("http.proxyPort", "8080");
		String endpoint = "http://120.193.39.85/webservice/services/sendmsg";
		Service service = new Service();
		Call call = null;

		call = (Call) service.createCall();

		call.setOperationName(new QName(
				"http://120.193.39.85/webservice/services/sendmsg",
				"sendmsg_st"));
		call.setTargetEndpointAddress(new java.net.URL(endpoint));
		String ret = (String) call.invoke(new Object[] { dbuser, message });
		// System.out.println("return value is " + ret);
		return ret;
	}

	public static String deliver(String wbip, String dbuser) throws Exception {

		// //System.setProperty("http.proxyHost", "10.70.237.88");
		// //System.setProperty("http.proxyPort", "8080");
		String endpoint = "http://120.193.39.85/webservice/services/deliver";
		Service service = new Service();
		Call call = null;

		call = (Call) service.createCall();

		call.setOperationName(new QName(
				"http://120.193.39.85/webservice/services/deliver", "deliver"));
		call.setTargetEndpointAddress(new java.net.URL(endpoint));
		String ret = (String) call.invoke(new Object[] { dbuser });
		// System.out.println("return value is " + ret);
		return ret;
	}

	public static String receipt_all(String dbuser) throws Exception {

		// //System.setProperty("http.proxyHost", "10.70.237.88");
		// //System.setProperty("http.proxyPort", "8080");
		String endpoint = "http://111.1.15.162/webservice/services/rt_receipt_all";
		Service service = new Service();
		Call call = null;

		call = (Call) service.createCall();

		call.setOperationName(new QName(
				"http://111.1.15.162/webservice/services/rt_receipt_all",
				"receipt"));
		call.setTargetEndpointAddress(new java.net.URL(endpoint));
		String ret = (String) call.invoke(new Object[] { dbuser });
		// System.out.println("return value is " + ret);
		return ret;
	}

	public static String rt_receipt(String dbuser, String msg_id)
			throws Exception {

		// //System.setProperty("http.proxyHost", "10.70.237.88");
		// //System.setProperty("http.proxyPort", "8080");
		String endpoint = "http://120.193.39.85/webservice/services/rt_receipt";
		Service service = new Service();
		Call call = null;

		call = (Call) service.createCall();

		call.setOperationName(new QName(
				"http://120.193.39.85/webservice/services/rt_receipt",
				"receipt"));
		call.setTargetEndpointAddress(new java.net.URL(endpoint));
		String ret = (String) call.invoke(new Object[] { dbuser, msg_id });
		// System.out.println("return value is " + ret);
		return ret;
	}

	public static String add_msg_t() throws Exception {
		String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<infos>";
		return message;
	}

	public static String add_msg_w() throws Exception {
		String message = "</infos>";
		return message;
	}

	public static String add_msg_b(String msg_id, String password,
			String src_tele_num, String dest_tele_num, String msg)
			throws Exception {
		String message = "<info>" + "<msg_id><![CDATA[" + msg_id
				+ "]]></msg_id>" + "<password><![CDATA[" + password
				+ "]]></password>" + "<src_tele_num><![CDATA[" + src_tele_num
				+ "]]></src_tele_num>" + "<dest_tele_num><![CDATA["
				+ dest_tele_num + "]]></dest_tele_num>" + "<msg><![CDATA["
				+ msg + "]]></msg>" + "</info>";
		return message;
	}

}